from PyQt6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from PyQt6.QtCore import Qt
from qfluentwidgets import (LineEdit, PushButton, CardWidget, 
                          FluentIcon as FIF, InfoBar, InfoBarPosition)

class RegisterWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()
        
    def setup_ui(self):
        self.setWindowTitle("用户注册")
        self.resize(500, 400)
        
        # 主布局
        layout = QVBoxLayout()
        card = CardWidget()
        card_layout = QVBoxLayout()
        
        # 用户名输入
        self.username = LineEdit()
        self.username.setPlaceholderText("请输入用户名")
        self.username.setIcon(FIF.PERSON)
        
        # 密码输入
        self.password = LineEdit()
        self.password.setPlaceholderText("请输入密码")
        self.password.setIcon(FIF.LOCK)
        self.password.setEchoMode(LineEdit.EchoMode.Password)
        
        # 确认密码
        self.confirm_password = LineEdit()
        self.confirm_password.setPlaceholderText("请确认密码")
        self.confirm_password.setIcon(FIF.LOCK)
        self.confirm_password.setEchoMode(LineEdit.EchoMode.Password)
        
        # 邮箱输入
        self.email = LineEdit()
        self.email.setPlaceholderText("请输入邮箱")
        self.email.setIcon(FIF.MAIL)
        
        # 按钮布局
        btn_layout = QHBoxLayout()
        
        # 注册按钮
        self.register_btn = PushButton("注册")
        self.register_btn.setIcon(FIF.SEND)
        
        # 返回按钮
        self.back_btn = PushButton("返回")
        self.back_btn.setIcon(FIF.RETURN)
        
        # 添加控件到布局
        card_layout.addSpacing(20)
        card_layout.addWidget(self.username)
        card_layout.addSpacing(10)
        card_layout.addWidget(self.password)
        card_layout.addSpacing(10)
        card_layout.addWidget(self.confirm_password)
        card_layout.addSpacing(10)
        card_layout.addWidget(self.email)
        card_layout.addSpacing(20)
        
        btn_layout.addWidget(self.back_btn)
        btn_layout.addWidget(self.register_btn)
        card_layout.addLayout(btn_layout)
        
        card.setLayout(card_layout)
        layout.addWidget(card)
        self.setLayout(layout)
        
        # 设置样式
        self.setStyleSheet("""
            QWidget {
                background-color: #f5f5f5;
            }
            
            LineEdit {
                padding: 8px;
                font-size: 14px;
                border-radius: 4px;
            }
            
            PushButton {
                padding: 8px 24px;
                font-size: 14px;
                font-weight: bold;
            }
        """)
        
        # 绑定事件
        self.register_btn.clicked.connect(self.do_register)
        self.back_btn.clicked.connect(self.close)
        
    def do_register(self):
        username = self.username.text()
        password = self.password.text()
        confirm_pwd = self.confirm_password.text()
        email = self.email.text()
        
        # 表单验证
        if not all([username, password, confirm_pwd, email]):
            self.show_info_bar("错误", "请填写所有字段", True)
            return
            
        if password != confirm_pwd:
            self.show_info_bar("错误", "两次密码输入不一致", True)
            return
            
        if len(password) < 6:
            self.show_info_bar("错误", "密码长度不能少于6位", True)
            return
            
        # TODO: 验证邮箱格式
        # TODO: 实现注册逻辑
        
        self.show_info_bar("成功", "注册成功！", False)
        self.close()
        
    def show_info_bar(self, title, content, is_error=False):
        """显示信息提示条"""
        InfoBar.success(
            title=title,
            content=content,
            orient=Qt.Orientation.Horizontal,
            isClosable=True,
            position=InfoBarPosition.TOP,
            duration=2000,
            parent=self
        ) if not is_error else InfoBar.error(
            title=title,
            content=content,
            orient=Qt.Orientation.Horizontal,
            isClosable=True,
            position=InfoBarPosition.TOP,
            duration=2000,
            parent=self
        ) 